// 系统主动推送到客户端数据处理
import { FAction } from '@/plugins/constants'
import { useChannelStore } from '@/store/channel'
import { useChatStore } from '@/store/chat'
import { useSessionStore } from '@/store/session'
import { find, remove } from 'lodash'
import { v7 as uuidv7 } from 'uuid'
import { closeToast } from 'vant'
import socketServer from './socketServer'

function handleSocketMessage(message: any) {
  const session = useSessionStore()
  const channel = useChannelStore()
  const chat = useChatStore()
  switch (message.contentType) {
    // 排队消息
    case 'queue_num':
      const queueNum = Number(message.content)
      // 如果有取消排队成功消息的话将其删除掉
      remove(chat.messages, (item) => item.state === 'VisitorClose')
      chat.updateQueueMessages({
        role: 'system',
        type: 'queue',
        state: 'queue_num',
        queueNum: queueNum,
        id: message.mid,
        uuid: message.messageUUID || uuidv7(),
      })
      session.updateQueueNum(queueNum)
      session.updateActionState(FAction.REQAGENT)
      closeToast()
      break
    // 排队即将超时
    case 'queue_timeout':
      chat.updateQueueMessages({
        role: 'system',
        type: 'queue',
        state: 'queue_timeout',
        queueNum: session.queueNum,
        content: message.content,
        id: message.mid,
        uuid: message.messageUUID || uuidv7(),
      })
      session.updateActionState(FAction.REQAGENT)
      break
    // 排队超时结束
    case 'queue_timeout_end':
      chat.removeQueueMessages()
      chat.updateMessages({
        role: 'system',
        type: 'text',
        state: 'timeout_end',
        content: message.content,
      })
      session.updateActionState(FAction.TIMEOUT)
      break
    //访客排队超时未回复退出
    case 'QueueTimeout':
      session.updateActionState(FAction.TIMEOUT)
      socketServer.closeSocket()
      break
    // 空闲坐席已接起
    case 'accessSeats':
      chat.updateMessages({
        role: 'system',
        type: 'text',
        state: 'accessSeats',
        content: message.content,
        id: message.mid,
        uuid: message.messageUUID || uuidv7(),
      })
      break
    // 坐席已接起展示欢迎语
    case 'visitorMeeting':
      // 正式开始连接上人工对话
      console.log('FAction', session.FAction)
      if (session.FAction === FAction.REQAGENT) {
        // 排队进入会话得把排队消息从对话列表中移除
        chat.removeQueueMessages()
        session.updateActionState(FAction.AGENT)
      } else {
        session.updateActionState(FAction.AGENT)
      }
      chat.updateMessages({
        role: 'seat',
        type: 'text',
        state: 'visitorMeeting',
        content: message.content,
        id: message.mid,
        uuid: message.messageUUID || uuidv7(),
      })
      break
    // 访客未回复超时退出警告
    case 'VisitorTimeoutWarning':
      chat.updateMessages({
        role: 'seat',
        type: 'text',
        state: 'VisitorTimeoutWarning',
        content: message.content,
        id: message.mid,
        uuid: message.messageUUID || uuidv7(),
      })
      break
    // 访客未回复超时退出
    case 'VisitorTimeout':
      chat.updateMessages({
        role: 'seat',
        type: 'text',
        state: 'VisitorTimeout',
        content: message.content,
        id: message.mid,
        uuid: message.messageUUID || uuidv7(),
      })
      socketServer.closeSocket()
      if (channel.SystemInfo.agentInvalid) {
        // 推送评价
        chat.updateMessages({
          role: 'system',
          type: 'evaluate',
          target: 'agent',
          evalType: 4,
          id: uuidv7(),
        })
      }
      break
    // 坐席主动结束会话
    case 'AgentDestroyMeeting':
      chat.updateMessages({
        role: 'seat',
        type: 'text',
        state: 'AgentDestroyMeeting',
        content: message.content,
        id: message.mid,
        uuid: message.messageUUID || uuidv7(),
      })
      socketServer.closeSocket()
      session.updateActionState(FAction.DESTROY)
      break
    // 坐席超时
    case 'AgentTimeOut':
      chat.updateMessages({
        role: 'system',
        type: 'text',
        state: 'AgentTimeOut',
        content: message.content,
        id: message.mid,
        uuid: message.messageUUID || uuidv7(),
      })
      socketServer.closeSocket()
      if (channel.SystemInfo.agentInvalid) {
        // 推送评价
        chat.updateMessages({
          role: 'system',
          type: 'evaluate',
          target: 'agent',
          evalType: 4,
          id: uuidv7(),
        })
      }
      session.updateActionState(FAction.DESTROY)
      break
    case 'SeatTimeoutWarning':
      chat.updateMessages({
        role: 'system',
        type: 'text',
        state: 'SeatTimeoutWarning',
        content: message.content,
        id: message.mid,
        uuid: message.messageUUID || uuidv7(),
      })
      break
    // 坐席邀评价
    case 'surveg_adding':
      // evalType: 1-机器人对话后定时销毁推送满意度 2-坐席邀评 3-访客主动评价 4-人工结束推送
      chat.updateMessages({
        role: 'system',
        type: 'evaluate',
        target: 'agent',
        evalType: 2,
        id: uuidv7(),
      })
      break
    // 取消排队
    case 'VisitorClose':
      if (session.FAction === FAction.VISITOR_LEAVE) {
        // 留言状态
        chat.updateMessages({
          role: 'system',
          type: 'leave',
          state: 'VisitorLeave',
          content: '请输入您的留言并发送',
          id: message.mid,
          uuid: message.messageUUID || uuidv7(),
        })
      } else {
        chat.updateMessages({
          role: 'system',
          type: 'text',
          state: 'VisitorClose',
          content: message.content,
          id: message.mid,
          uuid: message.messageUUID || uuidv7(),
        })
      }
      socketServer.closeSocket()
      break
    default:
      break
  }
}

function handleUserMessage(message: any) {
  const chat = useChatStore()
  const visitorToAgentMessage = find(chat.visitorToAgentMessage, {
    id: message.mid,
  })
  if (visitorToAgentMessage) {
    visitorToAgentMessage.status = 'success'
    chat.updateMessages(visitorToAgentMessage)
  } else {
    chat.updateMessages({
      role: 'seat',
      type: 'text',
      read: false,
      content: message.content,
      id: message.mid,
      uuid: message.messageUUID || uuidv7(),
    })
  }
}
export default handleSocketMessage
export { handleUserMessage }
